#include "Map.h"

using namespace std;

Cell::Cell(void) : numHumans(0), numVampires(0), numWerewolves(0)
{
}

Cell::~Cell()
{

}

Map::Map(int width, int height) : height(height), width(width), startX(0), startY(0), cells(height*width), race(HUMAN)
{
}

void Map::setHome(int x, int y)
{
	startX = x;
	startY = y;
}

Cell* Map::getStartCell(){
	return &cells[startX+ startY*width];
}

Cell* Map::getCell(int x, int y)
{
	return &cells[x + y*width];
}

Race Map::getRace()
{
	return race;
}

int Map::getHeigth()
{
	return height;
}

int Map::getWidth()
{
	return width;
}

void Map::toString(void)
{
	Cell *pCurrCell;
	cout << "Height: " << height << endl;
	cout << "Width: " << width << endl;
	cout << "Home: (" << startX << "," << startY << ")" << endl;
	for(int y = 0; y<height; y++)
	{
		for(int x = 0; x<width; x++)
		{
			pCurrCell = &cells[y*width+x];
			if(pCurrCell->numHumans != 0)
			{
				cout << pCurrCell->numHumans << "H ";
			} else if (pCurrCell->numVampires != 0){
				cout << pCurrCell->numVampires << "V ";
			} else if (pCurrCell->numWerewolves != 0) {
				cout << pCurrCell->numWerewolves << "W ";
			} else {
				cout << "0  ";
			}
		}
		cout << endl;
	}
}

void Map::update(char * updateChar, int cellsNumber)
{
	for(int i = 0; i < cellsNumber; i++)
	{
		int x = (int)updateChar[i*5];
		int y = (int)updateChar[i*5+1];
		int numHum = (int)updateChar[i*5+2];
		int numVamp = (int)updateChar[i*5+3];
		int numWere = (int)updateChar[i*5+4];
		cells[y*width+x].numHumans = numHum;
		cells[y*width+x].numVampires = numVamp;
		cells[y*width+x].numWerewolves= numWere;
	}
	if(race == HUMAN)
	{
		if(cells[startX + startY*width].numVampires > 0)
			race = VAMPIRE;
		else if(cells[startX + startY*width].numWerewolves > 0)
			race = WEREWOLF;
		else
			cout << "Error, no race defined" << endl;
	}
}

Map::~Map(void)
{
}
